import { useEffect, useState } from 'react'

import { orderListByNursingWorkersIdApi } from '@/api/pcs/pcsOrderStatistics'
import AekLinkButton from '@/components/AekLinkButton'
import AekModal, { PromiseThen } from '@/components/AekModal'
import AekTable from '@/components/AekTable'
import DetailDrawer from '@/pages/pcs/pcsCompanionOrder/components/DetailDrawer'
import { formatDate } from '@/utils/displayHelper'
import { PCS_COMBO_TYPE_ENUM, PCS_COMPANION_PERIOD_ENUM, PCS_COMPANION_ORDER_STATUS_ENUM } from '@/utils/enum'
import { requestWithTry } from '@/utils/request'

const InfoModal = ({ id }) => {
  const [content, setContent] = useState([])
  const [loading, setLoading] = useState(false)

  const getPageList = async () => {
    setLoading(true)
    const { data } = await requestWithTry(orderListByNursingWorkersIdApi, {
      nursingWorkersId: id,
    })
    setLoading(false)
    setContent(data)
  }

  useEffect(() => {
    getPageList()
  }, [])

  /** @type {import("@/components/AekTable").AekTableProps} */
  const tableProps = {
    configKey: 'pcsOrderStatistics-InfoModal',
    // showConfig: false,
    rowKey: ({ orderId, startDate, startPeriodRange }) => `${orderId}-${startDate}-${startPeriodRange}`,
    loading,
    dataSource: content,
    pagination: false,
    columns: [
      {
        dataIndex: 'orderNo',
        title: '订单号',
        render: (text, row) => (
          <AekLinkButton
            onClick={() => {
              DetailDrawer.PromiseThen({ id: row.orderId, hideOperate: true })
            }}
          >
            {text}
          </AekLinkButton>
        ),
      },
      { dataIndex: 'status', title: '订单状态', enum: PCS_COMPANION_ORDER_STATUS_ENUM },
      { dataIndex: 'patientName', title: '患者姓名' },
      {
        dataIndex: 'inpatientWardName',
        title: '地点',
        render: (text, row) => `${text} - ${row.bedNumber}床`,
      },
      { dataIndex: 'companionComboName', title: '套餐名称' },
      { dataIndex: 'comboType', title: '套餐类型', enum: PCS_COMBO_TYPE_ENUM },
      { dataIndex: 'serviceDays', title: '服务时长' },
      { dataIndex: 'createTime', title: '工单创建时间', isDateTime: true },
      {
        dataIndex: 'startDate',
        title: '服务开始时间',
        render: (text, { startPeriod }) => `${formatDate(text)} ${PCS_COMPANION_PERIOD_ENUM.getText(startPeriod)}`,
      },
      {
        dataIndex: 'endDate',
        title: '服务结束时间',
        render: (text, { endPeriod }) => `${formatDate(text)} ${PCS_COMPANION_PERIOD_ENUM.getText(endPeriod)}`,
      },
      { dataIndex: 'chargingStandard', title: '收费标准' },
      { dataIndex: 'totalAmount', title: '订单费用', isMoney: true },
      { dataIndex: 'nursingWorkerSettlement', title: '护工结算' },
      { dataIndex: 'settleTotalAmount', title: '结算费用', isMoney: true },
    ],
  }

  return (
    <AekModal title='订单信息' width={1000} showFooter={false}>
      <AekTable
        {...tableProps}
        autoSizeHeight={false}
        scroll={{
          y: 400,
        }}
      />
    </AekModal>
  )
}

InfoModal.PromiseThen = PromiseThen

export default InfoModal
